package mysql.entity;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.Instant;

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {
    @Id
    @GeneratedValue(generator = "uuid-generator")
    @GenericGenerator(name = "uuid-generator", strategy = "uuid")
    @Column(name = "ID", nullable = false, columnDefinition = "char(32)")
    private String id;

    @Version
    private Long version;

    @Column(name = "CREATE_TIME", updatable = false)
    @CreatedDate
    private Instant createTime;

    @Column(name = "CREATE_BY", updatable = false)
    @CreatedBy
    private String createBy;

    @Column(name = "UPDATE_TIME")
    @LastModifiedDate
    private Instant updateTime;

    @Column(name = "UPDATE_BY")
    @LastModifiedBy
    private String updateBy;

    @PrePersist
    public void prePersist() {
    }

    @PreUpdate
    public void preUpdate() {
    }
}
